package com.task.controller.custom;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.task.common.annotation.Log;
import com.task.common.entity.custom.Item;
import com.task.common.entity.custom.model.CreateItemModel;
import com.task.common.entity.custom.model.QueryItemModel;
import com.task.common.entity.custom.model.UpdateItemModel;
import com.task.common.enums.BusinessType;
import com.task.common.util.body.JsonResult;
import com.task.common.util.body.ResultTool;
import com.task.common.util.model.ModelUtil;
import com.task.service.IItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huzz
 * @since 2022-05-21
 */
@Api(tags = "项目管理")
@RestController
@RequestMapping("/bs/item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    @ApiOperation("创建一个项目")
    @PreAuthorize("@ss.hasPermi('add:item')")
    @Log(title = "创建一个项目",businessType = BusinessType.INSERT,isSaveRequestParam = true)
    @PostMapping("/createItem")
    public JsonResult createItem(CreateItemModel model){
        boolean result = itemService.createItem((Item) ModelUtil.toEntityStatic(model, Item.class));
        return result? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("根据id编辑")
    @PreAuthorize("@ss.hasPermi('update:item')")
    @Log(title = "根据id编辑",businessType = BusinessType.UPDATE,isSaveRequestParam = true)
    @PutMapping("/updateItem/{id}")
    public JsonResult updateItem(@PathVariable("id") Integer id, UpdateItemModel model){
        itemService.updateItem(id, (Item) ModelUtil.toEntityStatic(model, Item.class));
        return ResultTool.success();
    }

    @ApiOperation("项目列表")
    @PreAuthorize("@ss.hasPermi('query:item')")
    @Log(title = "项目列表",businessType = BusinessType.SELECT,isSaveRequestParam = true)
    @GetMapping("/findItemList")
    public JsonResult<IPage> findItemList(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSie,
                                          QueryItemModel model){
        return ResultTool.success(itemService.findItemList(pageNum, pageSie, (Item) ModelUtil.toEntityStatic(model, Item.class)));
    }

    @ApiOperation("删除一个项目")
    @PreAuthorize("@ss.hasPermi('del:item')")
    @Log(title = "删除一个项目",businessType = BusinessType.DELETE,isSaveRequestParam = true)
    @DeleteMapping("/deleteItem/{id}")
    public JsonResult deleteItem(@PathVariable("id") Integer id){
        return  itemService.deleteItem(id) ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("导入项目列表")
    @PreAuthorize("@ss.hasPermi('import:item')")
    @Log(title = "导入项目列表",businessType = BusinessType.IMPORT,isSaveRequestParam = true)
    @PostMapping("/importItem")
    public JsonResult importItem(@RequestParam("file") MultipartFile multipartFile){
        itemService.importItem(multipartFile);
        return ResultTool.success();
    }

    @ApiOperation("导出项目列表")
    @PreAuthorize("@ss.hasPermi('export:item')")
    @Log(title = "导出项目列表",businessType = BusinessType.EXPORT,isSaveRequestParam = true)
    @PostMapping("/exportItem")
    public void exportItem(HttpServletResponse response,
                                 HttpServletRequest request,
                                 @RequestBody QueryItemModel model){
        itemService.exportItem(request, response, (Item) ModelUtil.toEntityStatic(model, Item.class));
    }

}
